package com.heima.controller;

import com.heima.pojo.Dept;
import com.heima.pojo.Result;
import com.heima.service.DeptService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@Log

public class DeptController {

    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        List<Dept> depts = deptService.queryDepts();
        return Result.success(depts);
    }
    @DeleteMapping
    //这里相当于是起别名   用Integer的原因是如果不传的话用int会默认为0
    public Result delete(@RequestParam Integer id){

//        //service层处理删除逻辑
        deptService.delete(id);
        log.info(id+"========================================================================");

        return Result.success();
    }

    /**
     * 根据id修改部门参数
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    //绑定路径参数的一个注解
    public Result getById(@PathVariable Integer id){
        System.out.println(id);
        Dept byId = deptService.getById(id);
        return Result.success(byId);
    }

    /**
     * 添加部门
     * @param dept RequestBody表示是前端数据
     * @return
     */
    @PostMapping
    //因为是post请求,所以这里是接收请求体
    public Result add(@RequestBody Dept dept){
        System.out.println(dept+"=====");
        //交给service层
        deptService.add(dept);
        return Result.success();
    }
    /**
     * 修改部门信息
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        System.out.println(dept+"=====================================");
        deptService.update(dept);
        return Result.success();

    }

}
